%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% Any file used by the particular function whose handle is passed in the
% input argument "readFramesFunctHandle". Those may include:
% patientlyKeepTryingToReadUntilErrorFree.m 
% readAndCheckVideoFrames.m 
% readImageFile.m 
% readImagesByIndexRange.m 
% readVideoFrames.m 
% readdpx.m 
% Image processing toolbox
%
%==========================================================================

%==========================================================================
% The function "showFrameByIndex" shows the frame with a specified index in
% the frame viewer.
% INPUT: "projectStruct" is a project structure holding all information
% about the source of the frame and how to read it.
% "frameIndex" is an integer specifying the index of the frame to be shown.
% OUTPUT: None.
%==========================================================================
function showFrameByIndex(projectStruct, frameIndex)

% Read in the specified frame.
doConvertToGray = false;
%imageArr = projectStruct.readFramesFunctHandle(frameIndex, doConvertToGray);
imageArr = projectStruct.readFramesSafelyFunctHandle(frameIndex, doConvertToGray);

% Get the bit depth for that image.
bitDepth = projectStruct.workFile.bitDepth;

% Scale pixel values in case they are integers.
if (isinteger(imageArr) == true)
    % Compute the number of bits used to store each pixel value.
    storageBitDepth = round(log2(double(intmax(class(imageArr)))));
    
    % Check if the type used to store the pixel values can accommodate them.
    assert((storageBitDepth >= bitDepth), 'SoundRestoration:showFrameByIndex:TooSmallStorageType', ...
        'Type used to store the pixel values is too small to accommodate them.');
    
    % Scale pixel values to the full range offered by the type used to 
    % store them.
    scale = 2^(storageBitDepth - bitDepth);
    imageArr = imageArr*scale;
end

% Show the correctly scaled image.
imshow(imageArr);

return;
% end of the function "showFrameByIndex"
